tempfile r2s24 r3s24

* prepare round 2 *	
	use "$data2012/R2_Section_24", clear
	do "$do/analysis r2 s24_v1"
	
	keep if newHH==1
	
	foreach i in ch_agemo s24_q9 ch_haz06 ch_waz06 ch_whz06 anemic anemic_severe stunted underweight wasting {
		rename `i' r2_`i'
	}
	
	drop _m
	sort hhid idp_mother idp_child 
	save `r2s24', replace

* prepare round 3 *
	use "$data2013/R3_Section_24", clear
	do "$do/analysis r3 s24_v1"
	
	keep if newHH==1

	foreach i in s24_q9 ch_haz06 ch_waz06 ch_whz06 anemic anemic_severe stunted underweight wasting{
		rename `i' r3_`i'
	}
	
	drop _m
	sort hhid idp_mother idp_child 
	save `r3s24', replace

* merge r2 and r3 *
	use `r2s24', clear	
	merge 1:1 hhid idp_child using `r3s24', keep(mat) nogen

	foreach var in ch_haz06 ch_waz06 ch_whz06 {
		replace r2_`var'=. if abs(r2_`var')>6 
		replace r3_`var'=. if abs(r3_`var')>6 
		}
			
	foreach i in s24_q9 ch_haz06 ch_waz06 ch_whz06 anemic anemic_severe stunted underweight wasting{
		replace r3_`i'=. if r2_`i'==.
		replace r2_`i'=. if r3_`i'==.
	}

	replace r3_stunted=. 		if r3_ch_haz06==.
	replace r2_stunted=. 		if r2_ch_haz06==.
	replace r3_underweight=. 	if r3_ch_waz06==.
	replace r2_underweight=. 	if r2_ch_waz06==.
	replace r3_wasting=. 		if r3_ch_whz06==.
	replace r2_wasting=. 		if r2_ch_whz06==.
	
	foreach i in s24_q9 ch_haz06 ch_waz06 ch_whz06 anemic anemic_severe stunted underweight wasting {

		ge diff_`i'=r3_`i'-r2_`i'
		replace diff_`i'=. if r3_`i'==. | r2_`i'==.
	}

	* add diarrhea *
		sort hhid idp_mother idp_child
		merge m:1 hhid idp_mother idp_child using "$data2012/R2_Section_17", keep(mas mat) nogen keepusing(s17_q4 s17_q2)
	
	save "$do/children_new", replace 
